// Fill out your copyright notice in the Description page of Project Settings.


#include "QuickActorActionWidget.h"
#include "Subsystems/EditorActorSubsystem.h"
#include "SuperManager/DebugHeader.h"

void UQuickActorActionWidget::SelectAllActorsWithSimilarName()
{
	if(!GetEditorActorSubsystem()) return;

	TArray<AActor*> SelectedActors = EditorActorSubsystem->GetSelectedLevelActors();
	uint32 SelectionCounter = 0;

	if(SelectedActors.Num() == 0)
	{
		ShowNotifyInfo(TEXT("未选择演员"));
		return;
	}

	if(SelectedActors.Num() > 1)
	{
		ShowNotifyInfo(TEXT("您只能选择一个角色"));
		return;
	}

	FString SelectedActorName = SelectedActors[0]->GetActorLabel();
	const FString NameToSearch =  SelectedActorName.LeftChop(4);

	TArray<AActor*> AllLeveActors = EditorActorSubsystem->GetAllLevelActors();

	for(AActor* ActorInLevel:AllLeveActors)
	{
		if(!ActorInLevel) return;

		if(ActorInLevel->GetActorLabel().Contains(NameToSearch,SearchCase))
		{
			EditorActorSubsystem->SetActorSelectionState(ActorInLevel,true);
			SelectionCounter++;
		}
	}

	if(SelectionCounter>0)
	{
		ShowNotifyInfo(TEXT("成功的选择 ") + 
		FString::FromInt(SelectionCounter) + TEXT(" actors"));
	}
	else
	{
		ShowNotifyInfo(TEXT("没有找到相似名字的演员"));
	}
}

void UQuickActorActionWidget::DuplicateActors()
{
	if(!GetEditorActorSubsystem()) return;

	TArray<AActor*> SelectedActors = EditorActorSubsystem->GetSelectedLevelActors();
	uint32 Counter = 0;

	if(SelectedActors.Num() == 0)
	{
		ShowNotifyInfo(TEXT("未选择演员"));
		return;
	}

	if(NumberOfDuplicates <= 0 || OffsetDist == 0)
	{
		ShowNotifyInfo(TEXT("没有指定重复数或偏移距离"));
		return;
	}

	for (AActor* SelectedActor : SelectedActors)
	{
		if (!SelectedActor) return;

		for (int i = 0; i < NumberOfDuplicates; i++)
		{
			AActor* DuplicatedActor =
				EditorActorSubsystem->DuplicateActor(SelectedActor, SelectedActor->GetWorld());

			if (!DuplicatedActor) continue;

			const float DuplicationOffsetDist = (i + 1) * OffsetDist;

			switch (AxisForDuplication) {
			case E_DuplicationAxis::EDA_XAxis:
				
				DuplicatedActor->AddActorWorldOffset(FVector(DuplicationOffsetDist,0.f,0.f));
				break;
			case E_DuplicationAxis::EDA_YAxis:

				DuplicatedActor->AddActorWorldOffset(FVector(0.f,DuplicationOffsetDist,0.f));
				break;
			case E_DuplicationAxis::EDA_ZAxis:

				DuplicatedActor->AddActorWorldOffset(FVector(0.f,0.f,DuplicationOffsetDist));
				break;
			case E_DuplicationAxis::EDA_MAX:
				break;
			}

			EditorActorSubsystem->SetActorSelectionState(DuplicatedActor,true);
			Counter++;
		}
	}

	if(Counter>0)
	{
		ShowNotifyInfo(TEXT("成功复制 ")+
		FString::FromInt(Counter)+TEXT(" actors"));
	}

}

void UQuickActorActionWidget::RandomizeActorTransform()
{
	const bool bConditionNotSet = 
	!RandomActorRotation.bRandomizeRotYaw &&
	!RandomActorRotation.bRandomizeRotPitch &&
	!RandomActorRotation.bRandomizeRotRoll &&
	!bRandomizeScale &&
	!bRandomizeOffset;

	if(bConditionNotSet)
	{
		ShowNotifyInfo(TEXT("No variation condition specified"));
		return;
	}

	if(!GetEditorActorSubsystem()) return;

	TArray<AActor*> SelectedActors = EditorActorSubsystem->GetSelectedLevelActors();
	uint32 Counter = 0;

	if(SelectedActors.Num()==0)
	{
		ShowNotifyInfo(TEXT("No actor selected"));
		return;
	}

	for(AActor* SelectedActor:SelectedActors)
	{
		if(!SelectedActor) continue;

		if(RandomActorRotation.bRandomizeRotYaw)
		{
			const float RandomRotYawValue = FMath::RandRange(RandomActorRotation.RotYawMin,RandomActorRotation.RotYawMax);
			
			SelectedActor->AddActorWorldRotation(FRotator(0.f,RandomRotYawValue,0.f));
		}

		if(RandomActorRotation.bRandomizeRotPitch)
		{
			const float RandomRotPitchValue = FMath::RandRange(RandomActorRotation.RotPitchMin,RandomActorRotation.RotPitchMax);

			SelectedActor->AddActorWorldRotation(FRotator(RandomRotPitchValue,0.f,0.f));
		}

		if(RandomActorRotation.bRandomizeRotRoll)
		{
			const float RandomRotRollValue = FMath::RandRange(RandomActorRotation.RotRollMin,RandomActorRotation.RotRollMax);

			SelectedActor->AddActorWorldRotation(FRotator(0.f,0.f,RandomRotRollValue));
		}

		if(bRandomizeScale)
		{
			SelectedActor->SetActorScale3D(FVector(FMath::RandRange(ScaleMin,ScaleMax)));
		}

		if(bRandomizeOffset)
		{
			const float RandomOffsetValue = FMath::RandRange(OffsetMin,OffsetMax);

			SelectedActor->AddActorWorldOffset(FVector(RandomOffsetValue,RandomOffsetValue,0.f));
		}

		Counter++;
	}

	if(Counter>0)
	{
		ShowNotifyInfo(TEXT("Successfully set ")+
		FString::FromInt(Counter)+TEXT(" actors"));
	}

}

bool UQuickActorActionWidget::GetEditorActorSubsystem()
{
	if(!EditorActorSubsystem)
	{
		EditorActorSubsystem = GEditor->GetEditorSubsystem<UEditorActorSubsystem>();
	}
	return EditorActorSubsystem != nullptr;
}
